define(
["framework/asset/AssetSyst", "framework/math/transform", "render/sprite/SpriteBatch",
 "framework/graphic/WebGL"],
function(AssetSyst, Transform, SpriteBatch, WebGL) {

	return {
		create: function() {
			var _self = {
				init: function(systemMgr, texUrl) {
					//Load texture, on complete, load vertex data
					_texture 	= AssetSyst.get(texUrl, _createVertexData);
					_transform 	= Transform.create();
					_spriteBatch= systemMgr.getSystem(SpriteBatch);
					_spriteBatch.add(_self);

					//Return
					return _self;
				},

				destroy: function() {
					_spriteBatch.rem(_self);
					_destroyVertexData();
					AssetSyst.let(_texture);
				},

				render: function(shader) {
					if(!_vertices)
						return; 

					//Assign attribute
                    WebGL.pointAttrib(shader.getAttribute("aPos"), _vertices);
                    WebGL.pointAttrib(shader.getAttribute("aTex"), _texCoord);

                    //Get gl
                    var gl = WebGL.gl;

                    //Activate texture
                    _texture.uploadToGL();
                    gl.activeTexture(gl.TEXTURE0);
                    gl.bindTexture(gl.TEXTURE_2D, _texture.getGLTex());
                    gl.uniform1i(shader.getUniform("sampler_0"), 0);

                    //Assign transform
                    gl.uniformMatrix4fv(shader.getUniform("uMMat"), false, _transform.getMatrix());

                    //Draw call
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indices);
                    gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
				},

				getTransform: function() {
					return _transform;
				}
			};

			var _transform;
			var _spriteBatch;

			//Vertex data
			var _shader;
			var _vertices;
			var _texCoord;
			var _indices;
			var _texture;

			function _createVertexData() {
				//Create vertex data
				var hw = _texture.getWidth()/2;
				var hh = _texture.getHeight()/2;
				var vertex = [
					//BL
					-hw, -hh,  0, //Pos
					 
					//TL
					-hw,  hh,  0, //Pos
					 
					//TR
					 hw,  hh,  0, //Pos
					 
					//BR
					 hw, -hh,  0, //Pos
				];

				var texCoord = [
					//BL
					 0,  0,		//Tex coord
					 
					//TL
					 0,  1,		//Tex coord
					 
					//TR
					 1,  1,		//Tex coord
					 
					//BR
					 1,  0,		//Tex coord
				];

				var indices = [
					2, 1, 0,
					3, 2, 0
				];

				_vertices= WebGL.createStaticBuf(WebGL.gl.ARRAY_BUFFER, Float32Array, vertex, 3);
				_texCoord= WebGL.createStaticBuf(WebGL.gl.ARRAY_BUFFER, Float32Array, texCoord, 2);
				_indices = WebGL.createStaticBuf(WebGL.gl.ELEMENT_ARRAY_BUFFER, Uint16Array, indices, 1);
			}

			function _destroyVertexData() {
				throw "Unimplemented yet!";
			}

			return _self;
		}
	}
});